describe('TokenMASController', function(){
	var tokenMASController = null;

	beforeEach(function(){
		var t_appCtrl = App.ApplicationController.create();
		var t_alertCtrl = App.AlertController.create();
		var t_loadingCtrl = App.LoadingController.create();
		var t_moveHolderCtrl = App.MoveHolderController.create();
		
		App.TokenMASController.reopen({
			init:function(){
				this.set('controllers.application', t_appCtrl);
				this.set('controllers.alert', t_alertCtrl);
				this.set('controllers.loading', t_loadingCtrl);
				this.set('controllers.moveHolder', t_moveHolderCtrl);
			}
		});		
		
		tokenMASController = App.TokenMASController.create();
		
		// spyOn(App.User,  'login');
		// spyOn(App.User,  'logout');
		
		App.User.reopenClass({
			login : function(userName, pswd, cpch, callback) {},
			logout : function(callback){}
		});
		
		App.getSession().setAttribute('user', App.User.create());
	});
	
	describe('prepareController',function(){
		it("check for prepareController method",function(){
			tokenMASController.prepareController();
			spyOn(tokenMASController,'_clearMASToken');
			expect(App.getSession().getChallengeOpaque()).toBeDefined();
			expect(App.getSession().getSeedValue()).toBeDefined();
		});
		
		it("check for prepareController method for move",function(){
			tokenMASController.set('whichModule', 'move');
			tokenMASController.prepareController();
			spyOn(tokenMASController,'_clearMASToken');
			expect(App.getSession().getChallengeOpaque()).toBeDefined();
			expect(App.getSession().getSeedValue()).toBeDefined();
		});
	});
	
	describe('submitPIN',function(){
		it("If submit button can click, controller can call web service for validate 2FAToken", function(){
			tokenMASController.set('masToken', '123456');
			tokenMASController.set('submitBtnDisabled', false);
			
			spyOn(tokenMASController, 'showLoading');
			spyOn(App.api_alp,'validate2FAToken');
					
			tokenMASController.submitPIN();
			expect(tokenMASController.showLoading).toHaveBeenCalled();
			expect(App.api_alp.validate2FAToken).toHaveBeenCalled();
		});
		it("If submit button can click, controller can call web service for Transfer", function(){
			tokenMASController.set('masToken', '123456');
			tokenMASController.set('submitBtnDisabled', false);
			tokenMASController.set('whichModule', 'move')
			
			spyOn(tokenMASController, 'showLoading');
			spyOn(App.api_transfer,'confirmTransfer');
					
			tokenMASController.submitPIN();
			expect(tokenMASController.showLoading).toHaveBeenCalled();
			expect(App.api_transfer.confirmTransfer).toHaveBeenCalled();
		});
		it("If submit button cann't click, controller cann't call web service for validate 2FAToken", function(){
			tokenMASController.set('submitBtnDisabled', true);
			
			spyOn(tokenMASController, 'showLoading');
			spyOn(App.api_alp,'validate2FAToken');
			
			tokenMASController.submitPIN();
			expect(tokenMASController.showLoading).not.toHaveBeenCalled();
			expect(App.api_alp.validate2FAToken).not.toHaveBeenCalled();
		});
	});
	
	describe('_clearMASToken',function(){
		it("_clearMASToken method ",function(){
			expect(tokenMASController.get('masToken')).toBeNull();
			expect(tokenMASController.get('submitBtnDisabled')).toBeTruthy();
		});
	});
	
	describe('validate2FATokenCallback',function(){
		
		it("If without exception, set user to be validated and transition to 'see'", function(){
			var t_twoFA = {};
			var t_e = null;
			
			spyOn(tokenMASController,  'hideLoading');
			spyOn(tokenMASController,  'transitionTo');
			
			tokenMASController.validate2FATokenCallback(t_twoFA, t_e);
			
			expect(App.getSession().getAttribute('user').get('isValidated')).toBeTruthy();
			expect(tokenMASController.transitionTo).toHaveBeenCalled();
		});
		it("If it has exception and twoFA is undefined, should call prepareController and hideLoading", function(){
			var t_twoFA = null;
			var t_e = {};
			
			spyOn(tokenMASController,  'hideLoading');
			spyOn(tokenMASController,  'prepareController');
			spyOn(tokenMASController, 'hasError').andReturn(true);
			
			tokenMASController.validate2FATokenCallback(t_twoFA, t_e);
			
			expect(tokenMASController.prepareController).toHaveBeenCalled();
			expect(tokenMASController.hideLoading).toHaveBeenCalled();
		});		
		it("If it has exception and twoFA is not undefined, twoFA should update and should call prepareController and hideLoading", function(){
				var t_twoFA = {challenge : 'challenge',seedValue:'seedValue'};
				var t_e = {};
				
				spyOn(tokenMASController,  'hideLoading');
				spyOn(tokenMASController,  'prepareController');
				spyOn(tokenMASController, 'hasError').andReturn(true);
				
				tokenMASController.validate2FATokenCallback(t_twoFA, t_e);
				
				expect(App.getSession().getChallengeOpaque()).toEqual('challenge');
				expect(App.getSession().getSeedValue()).toEqual('seedValue');
				
				expect(tokenMASController.prepareController).toHaveBeenCalled();
				expect(tokenMASController.hideLoading).toHaveBeenCalled();
		});		
	});
	
	describe('confirmTransferCallback',function(){
		
		it("If without exception, user can transition to 'move.summary'", function(){
			var t_transferResult = {};
			var t_e = null;
			var t_transferResult = App.TransferResult.create();
			
			spyOn(tokenMASController,  'hideLoading');
			spyOn(App.getSession(),  'getCurrentTransfer').andReturn(t_transferResult);
			spyOn(tokenMASController,  '_clearMASToken');
			spyOn(tokenMASController,  'transitionTo');
			
			tokenMASController.confirmTransferCallback(t_transferResult, t_e);
			
			expect(tokenMASController.hideLoading).toHaveBeenCalled();
			expect(App.getSession().getCurrentTransfer).toHaveBeenCalled();
			expect(tokenMASController._clearMASToken).toHaveBeenCalled();
			expect(tokenMASController.transitionTo).toHaveBeenCalled();
		});
		it("If it has exception, should handle OTP", function(){
			var t_transferResult = {};
			var t_e = new BreezeException({
	        	errorCode: 123,
	        	errorMessage: 'error'
	        });
			var t_transferResult = App.TransferResult.create();
			
			spyOn(tokenMASController,  'hideLoading');
			spyOn(App.getSession(),  'getCurrentTransfer').andReturn(t_transferResult);
			spyOn(tokenMASController,  '_clearMASToken');
			spyOn(tokenMASController,  'otpErrorHandler');
			
			tokenMASController.confirmTransferCallback(t_transferResult, t_e);
			
			expect(tokenMASController.hideLoading).toHaveBeenCalled();
			expect(App.getSession().getCurrentTransfer).toHaveBeenCalled();
			expect(tokenMASController._clearMASToken).toHaveBeenCalled();
			expect(tokenMASController.otpErrorHandler).toHaveBeenCalled();
		});		
	});
	
	describe('otpErrorHandler',function(){
		
		it("If it has exception, should handle OTP", function(){
			var t_e = BreezeException.create({
	        	errorCode: 'ERR_SEC_RAE_OTP_COUNTER_EXCEEDED',
	        	errorMessage: 'error'
	        });
			
			spyOn(App.getSession(),  'getChallengeOpaque').andReturn('123');
			spyOn(App.getSession(),  'getSeedValue').andReturn('4567');
			spyOn(tokenMASController,  '_breakChallengeAndSeedValueForDisplay');
			spyOn(tokenMASController.get('controllers.alert'),  'showAlert');
			
			tokenMASController.otpErrorHandler(t_e);
			
			expect(App.getSession().getChallengeOpaque).toHaveBeenCalled();
			expect(App.getSession().getSeedValue).toHaveBeenCalled();
			expect(tokenMASController._breakChallengeAndSeedValueForDisplay).toHaveBeenCalled();
			expect(tokenMASController.get('controllers.alert').showAlert).toHaveBeenCalled();
		});
		
		it("If it has exception, should handle exception", function(){
			var t_e = BreezeException.create({
	        	errorCode: '123',
	        	errorMessage: 'error'
	        });
			
			spyOn(App.getSession(),  'getChallengeOpaque').andReturn('123');
			spyOn(App.getSession(),  'getSeedValue').andReturn('4567');
			spyOn(tokenMASController,  '_breakChallengeAndSeedValueForDisplay');
			spyOn(tokenMASController.get('controllers.alert'),  'showError');
			
			tokenMASController.otpErrorHandler(t_e);
			
			expect(App.getSession().getChallengeOpaque).toHaveBeenCalled();
			expect(App.getSession().getSeedValue).toHaveBeenCalled();
			expect(tokenMASController._breakChallengeAndSeedValueForDisplay).toHaveBeenCalled();
			expect(tokenMASController.get('controllers.alert').showError).toHaveBeenCalled();
		});
	});
	
	describe('_breakChallengeAndSeedValueForDisplay',function(){
		
		it("should break Challenge And SeedValue", function(){
			tokenMASController.set('challenge', '123');
			tokenMASController.set('seedValue', '4567');
			
			tokenMASController._breakChallengeAndSeedValueForDisplay();
			
			expect(tokenMASController.get('challenge_1')).toEqual('1');
			expect(tokenMASController.get('challenge_2')).toEqual('2');
			expect(tokenMASController.get('challenge_3')).toEqual('3');
			expect(tokenMASController.get('seedValue_1')).toEqual('4');
			expect(tokenMASController.get('seedValue_2')).toEqual('5');
			expect(tokenMASController.get('seedValue_3')).toEqual('6');
			expect(tokenMASController.get('seedValue_4')).toEqual('7');
		});
	});
});